import { ComponentType, ErrorInfo, PropsWithChildren, ReactNode } from 'react';

export type FallbackProps = {
  error: any;
  resetErrorBoundary: (...args: any[]) => void;
};

export type ErrorBoundaryProps = PropsWithChildren<{
  fallback: ReactNode | ComponentType<FallbackProps>;
  onError?: (error: Error, info: ErrorInfo) => void;
  onReset?: (
    details:
      | { reason: 'order'; args: any[] }
      | { reason: 'depend'; prev: any[] | undefined; next: any[] | undefined },
  ) => void;
  resetKeys?: any[];
}>;

export type ErrorBoundaryState =
  | {
      hasError: true;
      error: any;
    }
  | {
      hasError: false;
      error: null;
    };

export type UseErrorBoundaryState<T> =
  | { error: T; hasError: true }
  | { error: null; hasError: false };

export type UseErrorBoundaryApi<T> = {
  resetBoundary: () => void;
  showBoundary: (error: T) => void;
};

export type ErrorBoundaryContextType = {
  hasError: boolean;
  error: any;
  resetErrorBoundary: (...args: any[]) => void;
};
